<?php

namespace App\Http\Controllers\Admin;

use App\Models\SystemConfig;
use App\Models\User;
use App\Models\Vip;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class VipController extends Controller
{

    public function index()
    {
        $data       = Vip::getVipCardList();
        $vip_conf   = SystemConfig::getConfValByConfKey('vip_limit');
        return view('admin.vip.index',['lists'=>$data,'vip_conf'=>$vip_conf]);
    }


    public function modifyVipLimit($amount)
    {
        if (!is_numeric($amount)||$amount<0){
            return redirect('admin/vip/index')->with('danger','更新失败，输入不合法');
        }
        $amount = sprintf("%.2f",$amount);
        $flag   = SystemConfig::modifyValByConfKey('vip_limit',$amount);

        if($flag){
            return redirect()->back()->with('success','修改成功');
        }else{
            return redirect()->back()->with('danger','修改失败');
        }
    }

    public function disabled($id,$vip_card_status)
    {
        if (!is_numeric($id)||$id<0){
            return redirect('admin/vip/index')->with('danger','更新失败，输入不合法');
        }

        if (!in_array($vip_card_status,[0,1,2])){
            return redirect('admin/vip/index')->with('danger','更新失败，输入不合法');
        }
        $flag = Vip::updateById($id,['vip_card_status'=>$vip_card_status]);

        if($flag){
            return redirect()->back()->with('success','修改成功');
        }else{
            return redirect()->back()->with('danger','修改失败');
        }
    }


    public function add(Request $request)
    {
        if ($request->isMethod('post')){
            $input = $request->all();
            if (empty($input['vip_card_sn'])){
                return redirect()->back()->with('danger','卡号不能为空');
            }

            $data                   = [];
            $data['uid']            = $input['uid']??0;
            $data['vip_card_sn']    = $input['vip_card_sn'];
            $data['shop_name']      = $input['shop_name']??0;
            $flag                   = Vip::addVipCared($data);
            if($flag>0){
                return redirect('admin/vip/index')->with('success','修改成功');
            }else{
                return redirect('admin/vip/edit')->with('danger','修改失败');
            }
        }
        return view('admin.vip.add');
    }


    public function edit($id,Request $request)
    {

        if (!is_numeric($id)||$id<0){
            return redirect('admin/vip/index')->with('danger','更新失败，输入不合法');
        }

        if ($request->isMethod('get')){
            $data = Vip::getVipCardById($id);
            return view('admin.vip.add',['data'=>$data]);
        }

        if ($request->isMethod('post')){
            $input = $request->all();
            if (empty($input['vip_card_sn'])){
                return redirect()->back()->with('danger','卡号不能为空');
            }

            #限定 绑定会员卡的 必须是 达成充值条件 is_vip = 1
            if (!empty($input['uid'])&&$input['uid']>0){
                $user               = User::getUserInfoById($input['uid']);
                if ($user['is_vip'] != 1 ){
                    return redirect('admin/vip/index')->with('danger','该用户尚未达成会员充值限额条件，不能绑定会员卡');
                }
            }

            $data                   = [];
            $data['uid']            = $input['uid']??0;
            $data['vip_card_sn']    = $input['vip_card_sn'];
            $data['shop_name']      = $input['shop_name']??0;
            $flag                   = Vip::updateById($id,$data);
            if($flag>0){
                return redirect('admin/vip/index')->with('success','修改成功');
            }else{
                return redirect('admin/vip/index')->withwith('danger','修改失败');
            }
        }
    }



    public function unBind($id,Request $request)
    {
        if (!is_numeric($id)||$id<0){
            return redirect('admin/vip/index')->with('danger','更新失败，输入不合法');
        }
            $info                           = Vip::getVipCardById($id);
            if (empty($info['uid'])||$info['vip_card_status']!=1){
                return redirect('admin/vip/index')->with('success','会员卡状态错误');
            }
            $data                           = [];
            $data['uid']                    = '';
            $data['shop_name']              = '';
            $data['vip_card_status']        = 0;
            $flag                           = Vip::updateById($id,$data);
            $uflag                          = User::unBindVip($info['uid']);
            if($flag>0){
                return redirect('admin/vip/index')->with('success','修改成功');
            }else{
                return redirect('admin/vip/edit')->with('danger','修改失败');
            }
        }


}
